﻿using System;
using System.IO;
using System.Text;


namespace PublicFunction
{
    /// <summary>
    /// 日志功能
    /// </summary>
    public static class MyLogFunction
    {
        /// <summary>
        /// 报错信息日志
        /// </summary>
        public static string ErrorFilePath { get; private set; }
        private static readonly object LockPushFileUse = new();
        private static readonly object LockDevCommFileUse = new();
        private static readonly object LockErrorFileUse = new();
        private static readonly object LockOperatorFileUse = new();
        private static readonly object LockUserFileUse = new();
        static MyLogFunction()
        {
            ErrorFilePath = "C:\\Factory_MesConfig\\Factory_Mes1.0";
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="errFilePath"></param>
        /// <returns></returns>
        public static bool Initial(string errFilePath)
        {
            ErrorFilePath = errFilePath;

            return true;
        }

        /// <summary>
        /// 保存系统日志
        /// </summary>
        /// <param name="eventStr">事件信息</param>
        /// <param name="logInfo">日志信息</param>
        /// <param name="itemId">项目id</param>
        /// <param name="groupId">注册组id</param>
        public static void SaveSysEventLog(string eventStr, string logInfo, string itemId = "-", string groupId = "-")
        {
            try
            {
                string LineStr = string.Format("'{0}', '<{1}> : ''{2}', '{3}', '{4}'", DateTime.Now, eventStr, logInfo, itemId, groupId);
                string fullPath = string.Format("{0}\\Factory_MesLog_{1}_{2}.log", ErrorFilePath,
                    DateTime.Now.Year, DateTime.Now.Month);

                lock (LockErrorFileUse)
                {
                    FileInfo sysEventFileUse = new FileInfo(@fullPath);
                    if (!sysEventFileUse.Directory.Exists)
                    {
                        sysEventFileUse.Directory.Create();
                    }

                    StreamWriter sw = new StreamWriter(fullPath, true, Encoding.UTF8);

                    sw.WriteLine(LineStr);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

        /// <summary>
        /// 保存设备通讯日志.
        /// </summary>
        /// <param name="eventStr">事件信息.</param>
        /// <param name="logInfo">日志信息.</param>
        /// <param name="itemId">项目id.</param>
        /// <param name="groupId">注册组id.</param>
        public static void SaveDevCommLog(string eventStr, string logInfo, string itemId = "-", string groupId = "-")
        {
            try
            {
                string lineStr = string.Format("'{0}', '<{1}> : ''{2}', '{3}', '{4}'", DateTime.Now, eventStr, logInfo, itemId, groupId);
                string fullPath = string.Format("{0}\\XinjeCloud_DevComm_{1}_{2}.log", ErrorFilePath, DateTime.Now.Year, DateTime.Now.Month);

                SaveToFile(fullPath, lineStr, LockDevCommFileUse);
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

        /// <summary>
        /// 保存到文件.
        /// </summary>
        /// <param name="filePath">保存文件路径.</param>
        /// <param name="saveInfo">保存信息.</param>
        /// <param name="lockObject">操作锁.</param>
        public static void SaveToFile(string filePath, string saveInfo, object lockObject)
        {
            try
            {
                lock (lockObject)
                {
                    FileInfo sysEventFileUse = new(@filePath);
                    if (!sysEventFileUse.Directory.Exists)
                    {
                        sysEventFileUse.Directory.Create();
                    }

                    StreamWriter sw = new(filePath, true, Encoding.UTF8);

                    sw.WriteLine(saveInfo);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
                string str = ex.Message;
            }
        }

    }
}
